/*
 * Copyright (C) 2008-2010 Oliver Horn.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.funcj.tuples;

import java.io.Serializable;
import java.util.Arrays;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.googlecode.funcj.Transformable;

/**
 * A pair.
 *
 * A pair is an immutable sequence with two elements (or components). The types of both elements
 * can be different. Null values are permitted.
 *
 * @see Pairs
 *
 * @param <A> the type of the first element
 * @param <B> the type of the second element
 *
 * @author Oliver Horn
 */
@Immutable
public final class Pair<A, B> implements Tuple, Transformable<B>, Serializable {

	private static final long serialVersionUID = 2327744049909665364L;

	private final A first;
	private final B second;

	/**
	 * Constructs a pair with the given two elements.
	 * @param first the first element
	 * @param second the second element
	 */
	public Pair(@Nullable A first, @Nullable B second) {
		this.first = first;
		this.second = second;
	}

	/**
	 * Returns the first element of this pair.
	 * @return the first element
	 */
	public A first() {
		return first;
	}

	/**
	 * Returns the second element of this pair.
	 * @return the second element
	 */
	public B second() {
		return second;
	}

	/**
	 * {@inheritDoc}
	 */
	public <V> Pair<A, V> transform(Function<? super B, V> function) {
		return new Pair<A, V>(first, function.apply(second));
	}

	/**
	 * Returns a new {@code Pair} with swapped elements.
	 *
	 * Swapping a pair twice will return a pair which is equal to the original one.
	 * @return the swapped pair
	 */
	public Pair<B, A> swap() {
		return new Pair<B, A>(second, first);
	}

	/**
	 * {@inheritDoc}
	 * @return always {@code 2}
	 */
	public int arity() {
		return 2;
	}

	/**
	 * {@inheritDoc}
	 */
	public Object[] toArray() {
		return new Object[] {first, second};
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		return 961
			+ (first != null ? 31 * first.hashCode() : 0)
			+ (second != null ? second.hashCode() : 0);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof Pair) {
			Pair<?, ?> other = (Pair<?, ?>) obj;
			return Objects.equal(first, other.first) && Objects.equal(second, other.second);
		}
		if (obj instanceof Tuple) {
			Tuple other = (Tuple) obj;
			return (other.arity() == 2) && Arrays.equals(toArray(), other.toArray());
		}
		return false;
	}

	/**
	 * Returns a string representation of this pair.
	 * @return the string representation of this pair
	 */
	@Override
	public String toString() {
		return "(" + first + ", " + second + ")";
	}
}
